package b1.controls.basic
{
	import b1.net.*;
	import b1.scenes.*;
	import b1.scenes.home.*;
	import b1.utils.*;
	import b1.players.*;
	
	import flash.events.*;

	public class ExitIcon extends BasicButton
	{
		private var tip:BasicMessage;
		
		public function ExitIcon()
		{
			var EscapeIcon:Class = ResourceHelper.getInstance().getClassByName("EscapeIcon");
			super(EscapeIcon);
			this.width = 30;
			this.height = 30;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			if (tip == null)
			{
				tip = new BasicMessage(52, 25, "回家");
				tip.x = this.x + (30 - tip.width) / 2;
				tip.y = this.y + 40;
				this.parent.addChild(tip);
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			if (null !=  tip && null != tip.parent)
        	{
        		tip.parent.removeChild(tip);
        		tip = null;
        	}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var message:String;
			//在新手引导中
			if (SceneContainer.getInstance().step > 0)
			{
				message = "立即跳过新手引导？";
			}
			//在故事模式中
			else if (StoryHelper.getInstance().storyModeFlag)
			{
				message = "立即结束故事模式？";
			}
			//一般模式下
			else
			{
				message = "立即回家？";
			}
			var msgBox:MessageBox = new MessageBox(message, 1, true);
			msgBox.x = (BFont.stageWidth - msgBox.width) / 2;
			msgBox.y = (BFont.stageHeight - msgBox.height) / 2;
			msgBox.buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			SceneContainer.getInstance().topLayer.addChild(msgBox);
		}

		private function onButtonOKClick(event:MouseEvent):void
		{
			//在新手引导中
			if (SceneContainer.getInstance().step > 0)
			{
				SceneContainer.getInstance().stopNewHand();
			}
			//在故事模式中
			if (StoryHelper.getInstance().storyModeFlag)
			{
				StoryHelper.getInstance().endStory();
			}
			//一般模式下
			else
			{
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				home.switchLiveId(PlayerInfoHelper.getInstance().userId, true);
			}
		}
	}
}